#!/bin/bash
# Fail on errors
set -e

# GLOBAL VALUES 
DISTRIBUTION="llx1206"
INCOMING_POOL="zlx1206"
#INCOMING_POOL_APP="zlx1009APP"

# FILTER_LIST
FILTER_LIST_PREFIX="llx1206_"
FILTER_LIST_SUFFIX="-filterList.txt"
DISTRO_FINAL_DIR="/srv/nfs/lliurex1206/fllx1009/"
DISTRIBUTIONS="stable" 
DEBOOTSTRAP_DIR="/tmp/debootstrap-llx1206/"
DEBOOTSTRAP_FILE="debootstrap-llx1206.tar.gz"
BOOTSTRAP_LLIUREX="adduser apt apt-utils aptitude base-files base-passwd bash bsdutils busybox-initramfs bzip2 ca-certificates console-setup console-terminus coreutils cpio cron dash debconf debconf-i18n debianutils dhcp3-client dhcp3-common diffutils dmidecode dmsetup dpkg e2fslibs e2fsprogs eject file findutils gcc-4.4-base gnupg gpgv grep gzip hostname ifupdown initramfs-tools initramfs-tools-bin initscripts insserv iproute iputils-ping kbd klibc-utils laptop-detect less libacl1 libatm1 libattr1 libblkid1 libbz2-1.0 libc-bin libc6 libc6-i686 libcap2 libclass-accessor-perl libcomerr2 libcurl3-gnutls libcwidget3 libdb4.7 libdb4.8 libdbus-1-3 libdevmapper1.02.1 libept0 libfribidi0 libgcc1 libgcrypt11 libgdbm3 libglib2.0-0 libgnutls26 libgpg-error0 libgssapi-krb5-2 libidn11 libio-string-perl libk5crypto3 libkeyutils1 libklibc libkrb5-3 libkrb5support0 libldap-2.4-2 liblocale-gettext-perl liblockfile1 libmagic1 libncurses5 libncursesw5 libnewt0.52 libnih-dbus1 libnih1 libpam-modules libpam-runtime libpam0g libparse-debianchangelog-perl libpcre3 libplymouth2 libpng12-0 libpopt0 libreadline6 libsasl2-2 libsasl2-modules libselinux1 libsepol1 libsigc++-2.0-0c2a libslang2 libsqlite3-0 libss2 libssl0.9.8 libstdc++6 libsub-name-perl libtasn1-3 libtext-charwidth-perl libtext-iconv-perl libtext-wrapi18n-perl libtimedate-perl libudev0 libusb-0.1-4 libuuid1 libxapian15 lliurex-base-files locales lockfile-progs login lsb-base lsb-release lzma make makedev mawk mime-support module-init-tools mount mountall ncurses-base ncurses-bin net-tools netbase netcat-openbsd ntpdate openssl passwd perl perl-base perl-modules procps python python-central python-minimal python2.6 python2.6-minimal readline-common rsyslog lsb-lliurex-pinning sed sensible-utils sgml-base sudo sysv-rc sysvinit-utils tar tasksel tasksel-data tzdata ubuntu-keyring ubuntu-minimal ucf udev upstart ureadahead util-linux vim-common vim-tiny whiptail wget libdrm-intel1 libdrm-nouveau1 libdrm-radeon1 libplymouth2 libdrm2 xdg-user-dirs-jumble-data xkb-data zlib1g plymouth plymouth-theme-text libgpm2"
PACKAGES_FILE="/usr/share/lliurex-pool-data/all/lliurex-pool-data.list"
#PACKAGES_FILE="/usr/share/lliurex-pool-data/lists/essentials"
SUPPORTEDPACKAGES_FILE="/usr/share/lliurex-pool-data/lists/supported/cdd-supported"
METAPACKAGES_TRANSITIONALS="/tmp/metapackages.excluded"
METAPACKAGES_TRANSITIONALS_FINDER="pool-buscar-metapaquetes-transicionales.sh"

# SOURCES LIST
APT_SOURCES="http://pool.lliurex.fs/llx1206-unico llx1206 main restricted universe multiverse partner"

BLACKLIST_PACKAGES="childsplay-plugins lxnm"

sources_list() {
    echo "deb $APT_SOURCES"
}

remove_blacklisted_packages(){
   local sed_cmd
   for p in `echo $BLACKLIST_PACKAGES` ; do 
      sed_cmd="/^$p/D;$sed_cmd"
   done

   echo "$sed_cmd"
   return 0
}

make-debootstrap(){
	echo "Generamos el directorio "$DEBOOTSTRAP_DIR""
	mkdir -p "$DEBOOTSTRAP_DIR" || echo "No ha hecho falta, porque ya existe"
	echo "tar -xvzf "$DEBOOTSTRAP_FILE" "$DEBOOTSTRAP_DIR""
	exit 0
}

# Configuration variables
# ----
# MAIN
# ----
# SAVE CURRENT DIR
CDIR=`pwd`

# DIRECTORIES
BASE_DIR=`mktemp -t -d pool-$DISTRIBUTION.XXXXXX`
echo "WORKING DIRECTORY = '$BASE_DIR'"

# VARIABLES
if [ ! -d ${DEBOOTSTRAP_DIR} ] ; then
	echo "Vamos a generar el Debootstrap"
	make-debootstrap
fi

echo "Copiando el Debootstrap" 
rsync -ax ${DEBOOTSTRAP_DIR}  ${BASE_DIR} 

cd $BASE_DIR
mkdir -p "${BASE_DIR}/etc/apt/"
sources_list > ${BASE_DIR}/etc/apt/sources.list 
echo "Updating apt"
chroot ${BASE_DIR} apt-get update >/dev/null
echo "Installing lliurex-pool-data package remove lliurex-cdd packages"
chroot ${BASE_DIR} apt-get install --reinstall  -y lliurex-pool-data  &> /dev/null
echo "Reinstalling debootstrap packages"
for f in ${BOOTSTRAP_LLIUREX} ; do 
   chroot ${BASE_DIR}  apt-get install --reinstall -s -y $f  >> $BASE_DIR/lliurex-internal-debootstrap-install.log
done

echo "Installing Supported"
for m in `cat $BASE_DIR$SUPPORTEDPACKAGES_FILE`; do
  echo "Installing  $m"
  chroot ${BASE_DIR} apt-get -y -s install $m > $BASE_DIR/$m-install.log
done

echo "Installing lliurex metapackages"
for m in `cat $BASE_DIR$PACKAGES_FILE| grep lliurex-cdd`; do
  echo "Installing  $m"
  chroot ${BASE_DIR} apt-get -y -s install $m > $BASE_DIR/$m-install.log
done

cd $BASE_DIR
# Get the list of packages from the *-install.log
cat *-install.log | grep "^Inst" | sort -u > Inst.log
for d in $DISTRIBUTIONS; do
  grep "$d" Inst.log | awk '{ printf "%-38s  install\n", $2 }' | sort -u \
    > ${FILTER_LIST_PREFIX}pool-unico${FILTER_LIST_SUFFIX}
done
cd ${CDIR} 
# Copiamos los filtros que se han generado en todo este tinglado
cp $BASE_DIR/*.txt /srv/nfs/lliurex1206/fllx1206/conf/
#mv /srv/archive/lliurex1009/fllx1009/conf/llx1009_zlx1009APP-stable-filterList.txt /srv/archive/lliurex1009/fllx1009/conf/llx1009_lliurex-stable-filterList.txt
# Ejecutamos las operaciones de actualizacion del mirror de Fllx1009
reprepro -V -b /srv/nfs/lliurex1206/fllx1206/ --noskipold update 
reprepro -b /srv/nfs/lliurex1206/fllx1206/ retrack
reprepro -b /srv/nfs/lliurex1206/fllx1206/ export

# Ahora actualizamos el pool que sera LliureX net
reprepro -V -b /srv/nfs/lliurex1206/llx1206/ --noskipold update 
reprepro -b /srv/nfs/lliurex1206/llx1206/ retrack
reprepro -b /srv/nfs/lliurex1206/llx1206/ export

